﻿using System;
using System.Net;
using System.Windows.Browser;
using System.Windows.Controls;

namespace com.bodurov.SilverlightControls.XmlCodeEditorDemoApp
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            if(HtmlPage.Document.DocumentUri.AbsoluteUri.StartsWith("file"))
            {
                HtmlPage.Window.Alert("Please setup web project as a start project and default page as a start page");
                return;
            }

            var url = new Uri(@"../ContentTemplate.txt", UriKind.Relative);
            var client = new WebClient();
            client.DownloadStringCompleted += LoadIsComplete;
            client.DownloadStringAsync(url);

            HtmlPage.RegisterScriptableObject("MainPage", this);   
        }

        private void LoadIsComplete(object sender, DownloadStringCompletedEventArgs e)
        {
            CodeEditor.Text = e.Result;
        }




        [ScriptableMember]
        public void SetHtml(string html)
        {
            CodeEditor.Text = html;
        }
        [ScriptableMember]
        public string GetHtml()
        {
            return CodeEditor.Text;
        }
        [ScriptableMember]
        public void SetSize(double width, double height)
        {
            CodeEditor.Width = width;
            CodeEditor.Height = height;
        }
        [ScriptableMember]
        public double GetWidth()
        {
            return CodeEditor.Width;
        }
        [ScriptableMember]
        public double GetHeight()
        {
            return CodeEditor.Height;
        }
    }
}
